# Contributor: Frederic Wang <fred.wang@free.fr>

_realname=pbr
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=5.4.3
pkgrel=1
pkgdesc="Python Build Reasonableness (mingw-w64)"
url="https://pypi.python.org/pypi/pbr"
license=("Apache")
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-stestr"
              "${MINGW_PACKAGE_PREFIX}-python3-stestr"
              "${MINGW_PACKAGE_PREFIX}-python2-testscenarios"
              "${MINGW_PACKAGE_PREFIX}-python3-testscenarios"
              "${MINGW_PACKAGE_PREFIX}-python2-testresources"
              "${MINGW_PACKAGE_PREFIX}-python3-testresources"
              "${MINGW_PACKAGE_PREFIX}-python2-testrepository"
              "${MINGW_PACKAGE_PREFIX}-python3-testrepository"
              "${MINGW_PACKAGE_PREFIX}-python2-mock"
              "${MINGW_PACKAGE_PREFIX}-python3-mock"
              "${MINGW_PACKAGE_PREFIX}-python2-virtualenv"
              "${MINGW_PACKAGE_PREFIX}-python3-virtualenv"
              "${MINGW_PACKAGE_PREFIX}-python2-wheel"
              "${MINGW_PACKAGE_PREFIX}-python3-wheel"
              "${MINGW_PACKAGE_PREFIX}-python2-sphinx"
              "${MINGW_PACKAGE_PREFIX}-python3-sphinx")
options=('!strip')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/openstack-dev/pbr/archive/${pkgver}.tar.gz")
sha256sums=('533a774b5dc4c1fcb9712aaf248adca120800b835e64cfdf49157f7f7df82220')

prepare() {
  cd ${srcdir}
  sed -e 's/wheel.install/wheel.wheelfile/' \
      -e 's/wheel_file.zipfile/wheel_file/' \
      -i ${_realname}-$pkgver/pbr/tests/test_packaging.py
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH}| true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
  done
  find "${srcdir}/python2-build-${CARCH}" -name \*.py -exec sed -i '1s/python$/&2/' {} +
  find "${srcdir}/python2-build-${CARCH}" -name \*.py -exec sed -i '1s/python$/&3/' {} +
  export PBR_VERSION=$pkgver
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}" 
    cd "${srcdir}/python${pver}-build-${CARCH}"
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

check() {
  msg "Python ${pver} test for ${CARCH}"
  cd "${srcdir}/python${pver}-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    PYTHON=python2 ${MINGW_PREFIX}/bin/stestr2 run || warning "Tests failed"

  msg "Python 3 test for ${CARCH}"
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    PYTHON=python3 ${MINGW_PREFIX}/bin/stestr run || warning "Tests failed"
}

package_python3-pbr() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
  install=${_realname}3-${CARCH}.install

  cd ${srcdir}/python3-build-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}

package_python2-pbr() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")
  install=${_realname}2-${CARCH}.install

  cd ${srcdir}/python2-build-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done

  rename -v "pbr" "pbr2" "${pkgdir}${MINGW_PREFIX}"/bin/*
}

package_mingw-w64-i686-python2-pbr() {
  package_python2-pbr
}

package_mingw-w64-i686-python3-pbr() {
  package_python3-pbr
}

package_mingw-w64-x86_64-python2-pbr() {
  package_python2-pbr
}

package_mingw-w64-x86_64-python3-pbr() {
  package_python3-pbr
}
